//
//  DSSendCode.m
//  Created by admin on 17/3/24.
//  Copyright © 2017年 NiLaisong. All rights reserved.
//

#import "DSSendCode.h"
static DSSendCode * codeTime = nil;
dispatch_source_t _timer;
int timers;


@implementation DSSendCode

+ (void)GetsendCodeWithphone:(CodeTimeBlock)block{
    __block int time = 60;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(time<=0){
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
            NSString * string = [NSString stringWithFormat:@"%d",time];
            block(NO,string);
            });
        }else{
            dispatch_async(dispatch_get_main_queue(), ^{
                NSString * string = [NSString stringWithFormat:@"%d",time];
                block(YES,string);
            });
            time--;
        }
    });
    dispatch_resume(_timer);
}
+ (void)GetHoursCountDownWith:(CodeTimeBlock)block{
    __block int time = 5400;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(time<=0){
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                NSString * string = [NSString stringWithFormat:@"%.2d:%.2d",(time/60),(time%60)];
                NSLog(@"%@",string);
                block(NO,string);
            });
        }else{
            dispatch_async(dispatch_get_main_queue(), ^{
                NSString * string = [NSString stringWithFormat:@"%.2d:%.2d",(time/60),(time%60)];
                block(YES,string);
            });
            time--;
        }
    });
    dispatch_resume(_timer);
}
+(DSSendCode*)shareTime{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        codeTime = [[DSSendCode alloc] init];
    });
    return codeTime;
}

- (void)getsendCodeWithphone:(NSString *)phone :(CodeTimeBlock)block{
    if (phone&&self.phone!=phone){
        self.phone = phone;
        self.start = NO;
    }
    self.block = block;
    if (!self.start) {
        [self sendCodeTime];
    }
}
- (void)sendCodeTime{
    WeakSelf(self);
    if (_timer) {
        dispatch_resume(_timer);//TODO:第二次崩溃
    }
    timers = 60;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timers<=0){
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                weakSelf.start = NO;
                timers= 0;
                NSString * string = [NSString stringWithFormat:@"%d",timers];
                weakSelf.phone = nil;
                if (weakSelf.block) {
                    weakSelf.block(NO,string);
                }
            });
        }else{
            dispatch_async(dispatch_get_main_queue(), ^{
                weakSelf.start = YES;
                NSString * string = [NSString stringWithFormat:@"%d",timers];
                if (weakSelf.block) {
                    weakSelf.block(YES,string);
                }
            });
            timers--;
        }
    });
    dispatch_resume(_timer);
}
- (void)clears{
    timers = 0;
}
@end
